//
//  BabyCounterView.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-23.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "BabyCounterView.h"
#import "TextFieldView.h"
#import "SelectPickerView.h"
#import "NSString+DifferentColorString.h"

//婴儿出生日key
#define BABYCOUNTERVIEW_YE_BORN_DATE    [NSString stringWithFormat:@"%@_BABYCOUNTERVIEW_YE_BORN_DATE",APP_USER_MSG.id]

@interface BabyCounterView()<SelectPickerViewDelegate>
{
    UIScrollView *mainScroll;

    TextFieldView *selectField;
    TextFieldView *field1;
    TextFieldView *field2;
    TextFieldView *field3;
    TextFieldView *field4;
    
    UIButton *doneButton;
}
@end

@implementation BabyCounterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        mainScroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        [mainScroll setBounces:NO];
        [mainScroll setShowsVerticalScrollIndicator:NO];
        [self addSubview:mainScroll];
        
        field1 = [[TextFieldView alloc] init];
        [field1 setBgImage:@"输入框.png" titleImage:@"完善资料页-民族icon.png" tip:@"输入婴儿性别"];
        [mainScroll addSubview:field1];
        [field1 setCenter:CGPointMake(self.frame.size.width/2, 34+field1.frame.size.height/2)];
        
        //
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:field1.frame];
        [mainScroll addSubview:button];
        [button addTarget:self action:@selector(selectXB) forControlEvents:UIControlEventTouchUpInside];
        
        field2 = [[TextFieldView alloc] init];
        [field2 setBgImage:@"输入框.png" titleImage:@"完善资料页-出生日期icon.png" tip:@"输入婴儿出生日期"];
        [mainScroll addSubview:field2];
        [field2 setCenter:CGPointMake(field1.center.x, field1.frame.size.height+field1.frame.origin.y+16+field2.frame.size.height/2)];
        if ([APP_USERDEFAULTS valueForKey:BABYCOUNTERVIEW_YE_BORN_DATE]) {
            field2.textField.text = [APP_USERDEFAULTS valueForKey:BABYCOUNTERVIEW_YE_BORN_DATE];
        }
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button1 setFrame:field2.frame];
        [mainScroll addSubview:button1];
        [button1 addTarget:self action:@selector(selectDate) forControlEvents:UIControlEventTouchUpInside];
        

        field3 = [[TextFieldView alloc] init];
        [field3 setBgImage:@"输入框.png" titleImage:@"完善资料页-身高icon" tip:@"输入婴儿身长"];
        [mainScroll addSubview:field3];
        [field3 setCenter:CGPointMake(field2.center.x, field2.frame.size.height+field2.frame.origin.y+16+field3.frame.size.height/2)];
        
        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button2 setFrame:field3.frame];
        [mainScroll addSubview:button2];
        [button2 addTarget:self action:@selector(selectSG) forControlEvents:UIControlEventTouchUpInside];
        
        field4 = [[TextFieldView alloc] init];
        [field4 setBgImage:@"输入框.png" titleImage:@"完善资料页-孕前体重icon" tip:@"输入婴儿体重"];
        [mainScroll addSubview:field4];
        [field4 setCenter:CGPointMake(field3.center.x, field3.frame.size.height+field3.frame.origin.y+16+field4.frame.size.height/2)];
        
        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button3 setFrame:field4.frame];
        [mainScroll addSubview:button3];
        [button3 addTarget:self action:@selector(selectTZ) forControlEvents:UIControlEventTouchUpInside];
        
        doneButton = [OftenMenthod setButtonTitle:@"确定"];
        [mainScroll addSubview:doneButton];
        [doneButton addTarget:self action:@selector(doneButt) forControlEvents:UIControlEventTouchUpInside];
        [doneButton setCenter:CGPointMake(field4.center.x, field4.frame.size.height+field4.frame.origin.y+44+doneButton.frame.size.height/2)];
    }
    return self;
}

-(void)cleanView
{
    field1.textField.text = nil;
//    field2.textField.text = nil;
    field3.textField.text = nil;
    field4.textField.text = nil;
    [[mainScroll viewWithTag:110] removeFromSuperview];
    [[self.superview viewWithTag:111] removeFromSuperview];
    [mainScroll setContentOffset:CGPointMake(0, 0)];
}

-(void)getSelectView:(NSInteger)type
{
    UIView *view = self.superview;
    SelectPickerView *dateView = [[SelectPickerView alloc] initWithFrame:view.bounds];
    [dateView setDelegate:self];
    [dateView setTag:111];
    [dateView setShowView:type text:selectField.textField.text];
    [view addSubview:dateView];
}

-(void)selectXB
{
    CLog(@"选中婴儿性别");
    selectField = field1;
    [self getSelectView:SEX_TYPE];
}

-(void)selectDate
{
    CLog(@"选中婴儿出生日期");
    selectField = field2;
    [self getSelectView:YE_CSRQ_TYPE];
}

-(void)selectSG
{
    CLog(@"选择身高");
    selectField = field3;
    [self getSelectView:SG_YE_TYPE];
}

-(void)selectTZ
{
    CLog(@"选择体重");
    selectField = field4;
    [self getSelectView:YETZ_TYPE];
}

-(void)doneButt
{
    if (field1.textField.text.length == 0) {
        [LoadingFrameView autoDisappearTipview:@"请输入出生日期"];
    }else if (field2.textField.text.length == 0){
        [LoadingFrameView autoDisappearTipview:@"请输入性别"];
    }else if (field3.textField.text.length == 0){
        [LoadingFrameView autoDisappearTipview:@"请输入身高"];
    }else if (field4.textField.text.length == 0){
        [LoadingFrameView autoDisappearTipview:@"请输入体重"];
    }else{
        NSInteger month = [[NSDate date ] timeIntervalSinceDate:[z_DateTime dencodeTime:field2.textField.text format:APP_SELECTPICKERVIEW_DATE_FORMAT]]/24/3600/30;
        CLog(@"month==%d",month);
        if (month > 9) {
            [[mainScroll viewWithTag:110] removeFromSuperview];
            [LoadingFrameView autoDisappearTipview:@"以上测量针对9个月内的宝宝"];
        }else{
            NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"YE_JSQ" ofType:@"plist"]];
            NSDictionary *dataDic = [[dic objectForKey:field1.textField.text] objectForKey:[NSString stringWithFormat:@"%d",month]];
            CLog(@"dic===%@",dataDic);
            [self showResult:dataDic];
        }
    }
}

-(void)showResult:(NSDictionary*)dataDic
{
    [[mainScroll viewWithTag:110] removeFromSuperview];
    
    UIView *resultView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(doneButton.frame)+20, mainScroll.frame.size.width, 200)];
    [resultView setTag:110];
    [mainScroll addSubview:resultView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"result_bg.png"]];
    [imageView sizeToFit];
    [imageView setCenter:CGPointMake(15+imageView.frame.size.width/2, imageView.frame.size.height/2)];
    [resultView addSubview:imageView];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rule.png"]];
    [imageView1 sizeToFit];
    [imageView1 setCenter:CGPointMake(35+imageView1.frame.size.width/2, CGRectGetMaxY(imageView.frame)-imageView1.frame.size.height/2+10)];
    [resultView addSubview:imageView1];
    
    //身长
    CGFloat height = [[field3.textField.text substringToIndex:field3.textField.text.length-2] floatValue];
    NSString *sp1 = [self jslevel:height dic:[dataDic objectForKey:@"height"]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextColor:GET_COLOR(@"626262")];
    [label setFont:[UIFont boldSystemFontOfSize:13]];
    
    if (sp1.length > 0) {
        sp1 = [NSString stringWithFormat:@"✓%@",sp1];
        NSString *content = [NSString stringWithFormat:@"婴儿当前身长：%@  %@",field3.textField.text,sp1];
        label.attributedText = [content returnNSMutableAttributedString:sp1];
    }else{
        [label setText:[NSString stringWithFormat:@"婴儿当前身长：%@",field3.textField.text]];
    }
    
//    [label setText:[NSString stringWithFormat:@"婴儿当前身长：%@",field3.textField.text]];
    
    [label sizeToFit];
    [label setCenter:CGPointMake(CGRectGetMaxX(imageView1.frame)+10+label.frame.size.width/2, imageView1.center.y)];
    [resultView addSubview:label];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kg.png"]];
    [imageView2 sizeToFit];
    [imageView2 setCenter:CGPointMake(35+imageView2.frame.size.width/2, CGRectGetMaxY(imageView1.frame)+imageView2.frame.size.height/2+10)];
    [resultView addSubview:imageView2];
    
    //体重
    CGFloat weight = [[field4.textField.text substringToIndex:field4.textField.text.length-2] floatValue];
    NSString *sp2 = [self jslevel:weight dic:[dataDic objectForKey:@"weight"]];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
    [label2 setTextColor:GET_COLOR(@"626262")];
    [label2 setFont:[UIFont boldSystemFontOfSize:13]];
    
    if (sp2.length > 0) {
        sp2 = [NSString stringWithFormat:@"✓%@",sp2];
        NSString *content2 = [NSString stringWithFormat:@"婴儿当前体重：%@  %@",field4.textField.text,sp2];
        label2.attributedText = [content2 returnNSMutableAttributedString:sp2];
    }else{
        [label2 setText:[NSString stringWithFormat:@"婴儿当前体重：%@",field4.textField.text]];
    }
    
//    [label2 setText:[NSString stringWithFormat:@"婴儿当前体重：%@",field4.textField.text]];
    
    [label2 sizeToFit];
    [label2 setCenter:CGPointMake(CGRectGetMaxX(imageView2.frame)+10+label2.frame.size.width/2, imageView2.center.y)];
    [resultView addSubview:label2];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 245, 80)];
    [bgView setBackgroundColor:GET_COLOR(@"f6f6f6")];
    [bgView.layer setCornerRadius:4];
    [bgView setCenter:CGPointMake(mainScroll.frame.size.width/2, CGRectGetMaxY(imageView2.frame)+15+bgView.frame.size.height/2)];
    [resultView addSubview:bgView];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, bgView.frame.size.width-20, bgView.frame.size.height)];
    [label1 setTextColor:GET_COLOR(@"626262")];
    [label1 setFont:[UIFont systemFontOfSize:11]];
    [label1 setBackgroundColor:[UIColor clearColor]];
    [label1 setNumberOfLines:0];
    [label1 setText:@"注意：\n身长和体重是衡量宝宝健康的重要指标，如果处于低水平，要加强宝宝的营养啦~\n\n以上测量针对9个月内的宝宝。"];
    [bgView addSubview:label1];
    
    [mainScroll setContentSize:CGSizeMake(0, CGRectGetMaxY(bgView.frame)+resultView.frame.origin.y+20)];
    CLog(@"%@===%f",doneButton,CGRectGetMaxY(doneButton.frame));
}

-(NSString*)jslevel:(CGFloat)zhi dic:(NSDictionary*)dic
{
    NSInteger loc = 0;
    for (int i = 0 ; i < dic.count; i++) {
        CGFloat db = [[dic valueForKey:[NSString stringWithFormat:@"%d",i]] floatValue];
//        if ((zhi < db) || (i==(dic.count-1) && zhi==db)) {
        if ((zhi < db) || (i==(dic.count-1) && zhi>=db)) {
            loc = i;
            break;
        }
    }
    if (loc <= 1) {
        return @"处于低水平";
    }else if (loc == 2){
        return @"处于中等水平";
    }else if (loc >= 3){
        return @"处于高水平";
    }
    return @"";
}

#pragma mark - SelectPickerViewDelegate
//选择的时间
-(void)selectedText:(NSString*)text
{
    CLog(@"选中的=====%@",text);
    selectField.textField.text = text;
    if (selectField == field2) {
        [APP_USERDEFAULTS setValue:text forKey:BABYCOUNTERVIEW_YE_BORN_DATE];
    }
}

@end
